package meta

import (
	"github.com/micro-plat/hydra"
	"github.com/micro-plat/lib4go/types"
)

// AddPage 添加页面
func AddPage(tenant_id int, name, content string) (int64, error) {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"name":      name,
		"content":   content,
		"tenant_id": tenant_id,
	}
	id, _, err := db.Executes(sqlInsertPage, params)
	return id, err
}

// GetPageByID 根据ID获取页面信息
func GetPageByID(tenant_id int, id int64) (types.IXMap, error) {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"id":        id,
		"tenant_id": tenant_id,
	}
	data, err := db.Query(sqlGetPageByID, params)
	if err != nil {
		return nil, err
	}
	return data.Get(0), nil
}
func GetPageByName(tenant_id int, name string) (types.IXMap, error) {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"name":      name,
		"tenant_id": tenant_id,
	}
	data, err := db.Query(sqlGetPageByName, params)
	if err != nil {
		return nil, err
	}
	return data.Get(0), nil
}

// UpdatePage 根据ID更新页面信息
func UpdatePage(tenant_id int, id int64, name, content string) error {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"id":        id,
		"name":      name,
		"content":   content,
		"tenant_id": tenant_id,
	}
	_, err := db.Execute(sqlUpdatePage, params)
	return err
}
